home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / jcool01.zip / BASE_STA.H < prev    next >
C/C++ Source or Header  |  1992-09-23  |  6KB  |  136 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12. // Created: MBN 08/29/89 -- Initial design and implementation
  13. // Updated: VDN 02/21/92 -- New lite version
  14. // Updated: JAM 09/23/92 -- removed DOS specifics, stdized #includes
  15. // Updated: JAM 09/23/92 -- renamed from CoolStack to CoolBase_Stack
  16. // Updated: JAM 09/23/92 -- made *_state typedef a nested typedef "IterState"
  17. //                          as per new Iterator convention
  18. //
  19. // The CoolStack class is  publicly  derived from the  Generic class and is used to
  20. // implement non-type specific functionality for the parameterized CoolStack class.
  21. // In  this manner, code common  to  all instances  of the  CoolStack  class can be
  22. // shared to reduce code replication.  The  CoolStack<Type> class  implements a one
  23. // dimensional vector of a user-specified type.  This  is accomplished by using
  24. // the parameterized type  capability of C++.  The  stack will grow dynamically
  25. // as necessary  with   the amount  of  growth determined  by  the value  of an
  26. // allocation size slot.  Fixed length stacks are also supported by setting the
  27. // value of the allocation size slot to zero.
  28. //
  29. // Each CoolStack object contains a protected data section that has  a slot to hold
  30. // the current size of  the  stack,  a slot to  thold  the  number of  elements
  31. // currently on the  stack,  a static specificying  the allocation growth size,
  32. // and a float that may be set to some percentage to specify a growth ratio for
  33. // this instance of a queue
  34. //
  35. // There are three  constructors for the   CoolStack class.  The  first constructor
  36. // takes no arguments and creates an empty CoolStack object of the  specified type.
  37. // The second constructor takes an argument specifying the initial size  of the
  38. // stack.  Finally, the third constructor takes a single argument consisting of
  39. // a reference to a CoolStack and duplicates its size and values.
  40. //
  41. // The CoolStack class  provides generic, type-independent  methods to  remport the
  42. // number of items in  the stack, check the empty  status,  and clear all items
  43. // from the stack. The assignment operator is  overloaded and the three methods
  44. // to set the allocation growth size for the class as a whole, the growth ratio
  45. // for a specific instance of  a stack, and the length  (ie. the  largest valid
  46. // zero-relative index for random access) of the  stack are available. Finally,
  47. // exception handling functions  called by the parameterized CoolStack<Type>  class
  48. // are located   in the  base  class  to  facilitate code    charing  of common
  49. // functionality.
  50. //
  51.  
  52. #ifndef BASE_STACKH        // If no definition for CoolStack
  53. #define BASE_STACKH        // Define stack symbol
  54.  
  55. #ifndef STREAMH            // If the Stream support not yet defined,
  56. #include <iostream.h>        // include the Stream class header file
  57. #define STREAMH
  58. #endif
  59.  
  60. #ifndef MISCELANEOUSH        // If we have not included this file,
  61. #include <misc.h>        // include miscelaneous useful definitions.
  62. #endif
  63.  
  64. #define STACK_MEM_BLK_SZ 100
  65.  
  66. class CoolBase_Stack {
  67. protected:
  68.   long size;                    // Size of allocated storage
  69.   long number_elements;                // Number of elements in stack
  70.   static alloc_size_s;                // Allocation size for growth
  71.   float growth_ratio;                // If non-zero, growth ratio
  72.  
  73.   void top_error (const char*);            // Raise exception
  74.   void pop_error (const char*);            // Raise exception
  75.   void bracket_error (const char*, long);    // Raise exception
  76.   void push_error (const char*);        // Raise exception
  77.   void popn_error (const char*, long);        // Raise exception
  78.   void resize_error (const char*, long);    // Raise exception
  79.   void assign_error (const char*);        // Raise exception
  80.  
  81. public:
  82.   CoolBase_Stack ();                    // Simple constructor
  83.   CoolBase_Stack (long);                    // CoolBase_Stack of initial size
  84.   CoolBase_Stack (const CoolBase_Stack&);                // Duplicate another stack
  85.   ~CoolBase_Stack ();                    // CoolBase_Stack destructor
  86.  
  87.   CoolBase_Stack& operator= (const CoolBase_Stack& s);        // Assignment s = s2;
  88.  
  89.   inline Boolean is_empty () const;        // Is stack empty?
  90.   inline void clear ();                // Clears all values from stack
  91.   inline long length () const;            // Return number of stack items
  92.   inline long capacity() const;            // Max. number of elements
  93.  
  94.   long set_length (long, const char*);        // Set number of elements
  95.   void set_growth_ratio (float, const char*);    // Set growth percentage
  96.   void set_alloc_size (int, const char*);    // Set alloc size
  97. };
  98.  
  99.  
  100. // long length() -- Return the number of elements in this stack
  101. // Input:           None
  102. // Output:          Integer representing number of elements
  103.  
  104. inline long CoolBase_Stack::length () const {
  105.   return this->number_elements;
  106. }
  107.  
  108. // capacity -- Return maximum number of elements object can hold
  109. // Input:      None
  110. // Output:     Integer value of maximum number of elements
  111.  
  112. inline long CoolBase_Stack::capacity () const {
  113.   return (this->size);                // Return max number of values
  114. }
  115.  
  116.  
  117. // Boolean is_empty() -- Return TRUE if this stack is empty
  118. // Input:                None
  119. // Output:               TRUE or FALSE
  120.  
  121. inline Boolean CoolBase_Stack::is_empty () const {
  122.   return (this->number_elements == 0 ? TRUE : FALSE);
  123. }
  124.  
  125.  
  126. // void clear() -- Empty this stack  
  127. // Input:          None
  128. // Output:         None
  129.  
  130. inline void CoolBase_Stack::clear () {
  131.   this->number_elements = 0;
  132. }
  133.  
  134. #endif                        // End BASE_STACKH
  135.  
  136.